package com.sunyard.inser.sys.service.impl;


import java.util.List;
import java.util.Map;
import org.apache.dubbo.config.annotation.DubboService;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sunyard.inser.common.dto.InserException;
import com.sunyard.inser.dto.DataTablesResult;
import com.sunyard.inser.dto.Result;
import com.sunyard.inser.dto.PageInfo;
import com.sunyard.inser.common.enums.ErrorCode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sunyard.inser.sys.mapper.OrganInfoMapper;
import com.sunyard.inser.sys.entity.OrganInfo;
import com.sunyard.inser.sys.service.OrganInfoService;


@DubboService
public class OrganInfoServiceImpl implements OrganInfoService {

	private final static Logger logger = LoggerFactory.getLogger(OrganInfoServiceImpl.class);

	@Autowired
	private OrganInfoMapper organInfoMapper;

    @Override
    public DataTablesResult findList(PageInfo page, Map<String,Object> condition){

		DataTablesResult result = new DataTablesResult();
		
		try {
			PageHelper.startPage(page.getPageNum(), page.getPageSize());
			Page<OrganInfo> organInfoList = organInfoMapper.findList(condition);
			
			int total = organInfoMapper.count(condition).intValue();
			
			result.setData(organInfoList);
			result.setRecordsTotal(total);
			result.setCode(ErrorCode.SUCCESS.getCode());
			
		}catch (InserException e) {
			logger.error("findList error:", e);
			result.setCode(ErrorCode.ERROR.getCode());
			result.setMessage(e.getMessage());
		}

		return result;
    }
    
    @Override
    public Result getById(Long orgId){

		Result<OrganInfo> result = new Result<OrganInfo> ();
		
		try {
			OrganInfo organInfo = organInfoMapper.selectByPrimaryKey(orgId);
			result.setResult(organInfo);
			result.setCode(ErrorCode.SUCCESS.getCode());
			
		}catch (InserException e) {
			logger.error("getById error:", e);
			result.setCode(ErrorCode.ERROR.getCode());
			result.setMessage(e.getMessage());
		}
		return result;
    }
  
    @Override  
    public Result create(OrganInfo record){
    	Result result = new Result();
		
		try {
			organInfoMapper.insert(record);
			result.setCode(ErrorCode.SUCCESS.getCode());
		}catch (InserException e) {
			logger.error("create error:", e);
			result.setCode(ErrorCode.ERROR.getCode());
			result.setMessage(e.getMessage());
		}
		return result;
    }
    
    @Override 
    public Result update(OrganInfo record){
    	Result result = new Result();
		
		try {
			organInfoMapper.updateByPrimaryKeySelective(record);
			result.setCode(ErrorCode.SUCCESS.getCode());
		}catch (InserException e) {
			logger.error("update error:", e);
			result.setCode(ErrorCode.ERROR.getCode());
			result.setMessage(e.getMessage());
		}
		return result;
    }
    
    @Override
    public Result deleteById(Long orgId){

		Result result = new Result();
		
		try {
			organInfoMapper.deleteByPrimaryKey(orgId);
			result.setCode(ErrorCode.SUCCESS.getCode());
		}catch (InserException e) {
			logger.error("getById error:", e);
			result.setCode(ErrorCode.ERROR.getCode());
			result.setMessage(e.getMessage());
		}
		return result;
    }
        
    @Override
    public Result deleteBatch(List<Long> orgIds){

		Result result = new Result();
		
		try {
			organInfoMapper.deleteBatch(orgIds);
			result.setCode(ErrorCode.SUCCESS.getCode());
		}catch (InserException e) {
			logger.error("getById error:", e);
			result.setCode(ErrorCode.ERROR.getCode());
			result.setMessage(e.getMessage());
		}
		return result;
    }

}